#!/bin/bash

set -e

source "$(dirname $0)/parse_args" "$@"
source "$(dirname $0)/android-sdk"

if isHelp; then
  echo "usage: rust-bindgen [-h | --help] [all | native | wasm | android | ios]"
  echo ""
  echo "Generate Rust bindings for target platforms specified in arguments"
  echo "You can specify multiple targets at once"
  exit 0
fi

TARGET_NAME="libwallet_core_rs.a"
TARGET_XCFRAMEWORK_NAME=../swift/WalletCoreRs.xcframework
BUILD_FOLDER=../rust/target
CRATE="wallet-core-rs"
HEADER_NAME="WalletCoreRSBindgen.h"

create_xc_framework() {
  rm -rf $TARGET_XCFRAMEWORK_NAME
  xcodebuild -create-xcframework -library $BUILD_FOLDER/$TARGET_NAME -library $BUILD_FOLDER/darwin_universal/$TARGET_NAME -library $BUILD_FOLDER/aarch64-apple-ios/release/$TARGET_NAME -output $TARGET_XCFRAMEWORK_NAME
  mkdir -p $TARGET_XCFRAMEWORK_NAME/ios-arm64_x86_64-maccatalyst
  cp $BUILD_FOLDER/catalyst/$TARGET_NAME $TARGET_XCFRAMEWORK_NAME/ios-arm64_x86_64-maccatalyst
}

cd rust

if isTargetSpecified "native"; then
  echo "Generating Native target"
  cargo build --release --lib
fi

export RUSTFLAGS="-Zlocation-detail=none"

if isTargetSpecified "wasm"; then
  echo "Generating WASM target"

  source ../emsdk/emsdk_env.sh
  cargo build -Z build-std=std,panic_abort --target wasm32-unknown-emscripten --release --lib
fi

if isTargetSpecified "android"; then
  NDK_BIN_PATH=$(find_android_ndk)

  export AR="$NDK_BIN_PATH/llvm-ar"
  export CC_aarch64_linux_android="$NDK_BIN_PATH/aarch64-linux-android$NDK_API_LEVEL-clang"
  export CC_x86_64_linux_android="$NDK_BIN_PATH/x86_64-linux-android$NDK_API_LEVEL-clang"
  export CC_i686_linux_android="$NDK_BIN_PATH/i686-linux-android$NDK_API_LEVEL-clang"
  export CC_armv7_linux_androideabi="$NDK_BIN_PATH/armv7a-linux-androideabi$NDK_API_LEVEL-clang"

  echo "Generating Android targets"
  cargo build -Z build-std=std,panic_abort --target aarch64-linux-android --target armv7-linux-androideabi --target x86_64-linux-android --target i686-linux-android --release --lib
fi

if isTargetSpecified "ios" && [[ $(uname -s) == "Darwin" ]]; then
  echo "Generating iOS targets"
  cargo build -Z build-std=std,panic_abort --target aarch64-apple-ios --target aarch64-apple-ios-sim --target x86_64-apple-ios --target aarch64-apple-darwin --target x86_64-apple-darwin --target aarch64-apple-ios-macabi --target x86_64-apple-ios-macabi --release --lib &
  wait
  lipo $BUILD_FOLDER/x86_64-apple-ios/release/$TARGET_NAME $BUILD_FOLDER/aarch64-apple-ios-sim/release/$TARGET_NAME -create -output $BUILD_FOLDER/$TARGET_NAME
  mkdir -p $BUILD_FOLDER/darwin_universal
  lipo $BUILD_FOLDER/x86_64-apple-darwin/release/$TARGET_NAME $BUILD_FOLDER/aarch64-apple-darwin/release/$TARGET_NAME -create -output $BUILD_FOLDER/darwin_universal/$TARGET_NAME
  mkdir -p $BUILD_FOLDER/catalyst
  lipo $BUILD_FOLDER/aarch64-apple-ios-macabi/release/$TARGET_NAME $BUILD_FOLDER/x86_64-apple-ios-macabi/release/$TARGET_NAME -create -output $BUILD_FOLDER/catalyst/$TARGET_NAME

  create_xc_framework
fi

cbindgen --crate $CRATE --output ../src/rust/bindgen/$HEADER_NAME
cd -
[[ -e rust/target/release/${TARGET_NAME} ]] && cp rust/target/release/${TARGET_NAME} build/local/lib/

echo "Generating C++ files..."
pushd codegen-v2 && cargo run -- cpp && popd

if isTargetSpecified "ios" && [[ $(uname -s) == "Darwin" ]]; then
cd rust
cat > $TARGET_XCFRAMEWORK_NAME/Info.plist << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>AvailableLibraries</key>
	<array>
		<dict>
			<key>LibraryIdentifier</key>
			<string>macos-arm64_x86_64</string>
			<key>LibraryPath</key>
			<string>libwallet_core_rs.a</string>
			<key>SupportedArchitectures</key>
			<array>
				<string>arm64</string>
				<string>x86_64</string>
			</array>
			<key>SupportedPlatform</key>
			<string>macos</string>
		</dict>
		<dict>
        			<key>LibraryIdentifier</key>
        			<string>ios-arm64_x86_64-maccatalyst</string>
        			<key>LibraryPath</key>
        			<string>libwallet_core_rs.a</string>
        			<key>SupportedArchitectures</key>
        			<array>
        				<string>arm64</string>
        				<string>x86_64</string>
        			</array>
        			<key>SupportedPlatform</key>
        			<string>ios</string>
        			<key>SupportedPlatformVariant</key>
        			<string>maccatalyst</string>
        </dict>
		<dict>
			<key>LibraryIdentifier</key>
			<string>ios-arm64</string>
			<key>LibraryPath</key>
			<string>libwallet_core_rs.a</string>
			<key>SupportedArchitectures</key>
			<array>
				<string>arm64</string>
			</array>
			<key>SupportedPlatform</key>
			<string>ios</string>
		</dict>
		<dict>
			<key>LibraryIdentifier</key>
			<string>ios-arm64_x86_64-simulator</string>
			<key>LibraryPath</key>
			<string>libwallet_core_rs.a</string>
			<key>SupportedArchitectures</key>
			<array>
				<string>arm64</string>
				<string>x86_64</string>
			</array>
			<key>SupportedPlatform</key>
			<string>ios</string>
			<key>SupportedPlatformVariant</key>
			<string>simulator</string>
		</dict>
	</array>
	<key>CFBundlePackageType</key>
	<string>XFWK</string>
	<key>XCFrameworkFormatVersion</key>
	<string>1.0</string>
</dict>
</plist>
EOF
cd -
fi

